package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.RoleDao;
import com.qfedu.mtl.entity.Roles;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanHandler;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;
import com.qfedu.mtl.vo.TreeVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @Date 2023/9/26 19:22
 * @Author DARKER
 * @Email 1402216045@qq.com
 */
public class RoleDaoImpl implements RoleDao {
    //检查信息是否重复
    @Override
    public boolean checkInfoIsRepeat(Roles roles) {
        String sql = "select * from tb_roles where role_name=?";
        Roles roles1 = DBUtil.executeDQL(sql, DBUtil.getConn(),new BeanHandler<>(Roles.class, true), roles.getRoleName());
        if (roles1!=null){
            return true;
        }
        return false;
    }
    //插入角色
    @Override
    public int insert(Roles roles) {
        String sql = "insert into tb_roles(role_name,role_desc) values(?,?)";
        return DBUtil.executeDML(sql,DBUtil.getConn(),roles.getRoleName(),roles.getRoleDesc());
    }

    @Override
    public int delete(String id) {
        String sql = "delete from tb_roles where role_id=?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),id);
    }

    @Override
    public int update(Roles roles) {
        String sql = "update tb_roles set role_name=?,role_desc=? where role_id=?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),roles.getRoleName(),roles.getRoleDesc(),roles.getRoleId());
    }

    /**
     * 分页查找
     * @param page
     * @param size
     * @param role
     * @return
     */
    @Override
    public List<Roles> findAll(Integer page, Integer size, Roles role) {
        List<Object> values = new ArrayList<>();
        StringBuilder sqlBuilder= new StringBuilder("select * from tb_roles where 1=1 ");
        if (!StrUtil.isNullOrEmpty(role.getRoleName())){
            sqlBuilder.append(" and role_name like concat('%',?,'%')");
            values.add(role.getRoleName());
        }
        sqlBuilder.append(" limit ?,?");
        values.add((page - 1) * size);
        values.add(size);
        return DBUtil.executeDQL(sqlBuilder.toString(),DBUtil.getConn(), new BeanListHandler<>(Roles.class,true),values.toArray());
    }
    @Override
    public int selectTotal(Roles role) {
        List<Object> values = new ArrayList<>();
        StringBuilder sqlBuilder= new StringBuilder("select count(*) from tb_roles where 1=1 ");
        if (!StrUtil.isNullOrEmpty(role.getRoleName())){
            sqlBuilder.append(" and role_name like concat('%',?,'%')");
            values.add(role.getRoleName());
        }
        return DBUtil.executeDQL(sqlBuilder.toString(),DBUtil.getConn(), new NumberHandler<>(Long.class),values.toArray()).intValue();

    }
    //查找角色列表
    @Override
    public List<Roles> findSimpleRole() {
        String sql = "select * from tb_roles";
        return DBUtil.executeDQL(sql, DBUtil.getConn(),new BeanListHandler<>(Roles.class, true));
    }

    @Override
    public void delBatchById(List<Integer> ids) {
        StringBuilder sql = new StringBuilder("delete from tb_roles where role_id=? ");
        for (int i = 0; i < ids.size()-1; i++) {
            sql.append(" or role_id=? ");
        }
        DBUtil.executeDML(sql.toString(),DBUtil.getConn(),ids.toArray());

    }

    @Override
    public List<TreeVO> selectAllMenus() {
        String sql = "select menu_id as id, menu_name as title, menu_code as code , parent_menu_code as parentCode from tb_menus";
        return  DBUtil.executeDQL(sql,DBUtil.getConn(),new BeanListHandler<>(TreeVO.class,false));
    }

    @Override
    public void insertRoleMenu(Integer roleId, List<Integer> menuIds) {
        List<Object> values = new ArrayList<>();
        StringBuilder sql = new StringBuilder("insert into tb_role_menu(role_id,menu_id) values(?,?) ");
        values.add(roleId);
        values.add(menuIds.get(0));
        for (int i = 1; i < menuIds.size(); i++) {
            sql.append(",(?,?)");
            values.add(roleId);
            values.add(menuIds.get(i));
        }
        DBUtil.executeDML(sql.toString(),DBUtil.getConn(),values.toArray());

    }


}
